package com.api.devices.service.impl;

import com.api.common.enums.DataSourceType;
import com.api.devices.domain.query.tracing.evaluation.EvaluationScroeQuery;
import com.api.devices.domain.query.tracing.evaluation.EvaluationSeriesQuery;
import com.api.devices.domain.query.tracing.intergration.IntergrationQuery;
import com.api.devices.domain.vo.tracing.evaluation.EvalScoreVo;
import com.api.devices.domain.vo.tracing.intergration.IntergrationVo;
import com.api.devices.domain.vo.tree.TreeVo;
import com.api.devices.mapper.EvaluationMapper;
import com.api.devices.service.IEvaluationService;
import com.api.framework.datasource.DynamicDataSourceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EvaluationServiceImpl implements IEvaluationService {
    @Autowired
    EvaluationMapper evaluationMapper;

    @Override
    public List<Double> generates(TreeVo vo) {
        String tableName = vo.getEvaluationTabName();
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<Double> dataList = new ArrayList<>();
        try {
            dataList = evaluationMapper.generates(tableName, vo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return dataList;
    }

    @Override
    public List<EvaluationScroeQuery> scroe(EvalScoreVo vo) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        vo.initTableName();
        List<EvaluationScroeQuery> dataList = new ArrayList<>();
        try {
            dataList = evaluationMapper.scroe(vo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return dataList;
    }

    @Override
    public EvaluationSeriesQuery series(EvalScoreVo vo) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        EvaluationSeriesQuery data = new EvaluationSeriesQuery();

        vo.initTableName();
        try {
            data = evaluationMapper.series(vo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return data;
    }
}
